﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class employeeadd : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
                DataSet ds = new DataSet();

                if (user["IsBOOper"] != DBNull.Value && Convert.ToBoolean(user["IsBOOper"]))
                {
                    Hashtable bo = PhoneBook.Group.BranchOffice.get(Commons.StrUtil.FormatValue(user["bo"]));

                    ddlBo.Items.Add(new ListItem(Commons.StrUtil.FormatValue(bo["boname"]), Commons.StrUtil.FormatValue(bo["id"])));

                    ds = PhoneBook.Group.Department.list(Commons.StrUtil.FormatValue(user["groupid"]), bo["id"].ToString());
                    Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");
                }
                else
                {
                    ds = PhoneBook.Group.BranchOffice.list(Commons.StrUtil.FormatValue(user["groupid"]));
                    Commons.ControlUtil.BindDDL(ddlBo, ds, "boname", "id", "非分公司", "");

                    ds = PhoneBook.Group.Department.listforunit(Commons.StrUtil.FormatValue(user["groupid"]));
                    Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");
                }

                
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));

            Hashtable employee = new Hashtable();
            employee["employeename"] = txb_employeename.Text;
            employee["age"] = txb_age.Text;
            employee["departmentid"] = ddl_department.SelectedValue;
            employee["mobile"] = txb_mobile.Text;
            employee["phone"] = txb_phone.Text;
            employee["email"] = txb_email.Text;
            employee["shortnum"] = txb_shortnum.Text;

            employee["qq"] = txb_qq.Text;
            employee["msn"] = txb_msn.Text;
            employee["fax"] = txb_fax.Text;
            employee["duty"] = txb_duty.Text;
            employee["boid"] = string.IsNullOrWhiteSpace(ddlBo.SelectedValue) ? null : ddlBo.SelectedValue;

            Hashtable ht = PhoneBook.Group.Employee.getpercent(Commons.StrUtil.FormatValue(user["groupid"]));

            if (decimal.Parse(ht["empercent"].ToString()) >= 1M)
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.error, "人员数量已达到集团通讯录容量上限，请升级集团通讯录服务");
                return;
            }

            PhoneBook.Group.Employee.add(employee);
            string strTipMsg = string.Format("保存成功,可开通{0}个用户,已开通{1}个用户", ht["totalnum"], Convert.ToInt32(ht["emcount"]) + 1);
            //Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
            Helper.ShowMessage(u_message, controls.UCMessageType.success, strTipMsg);
            AddLog("新添加了人员：{0};", employee["employeename"]);
        }

        protected void ddlBo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(ddlBo.SelectedValue))
            {
                Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
                DataSet ds = PhoneBook.Group.Department.listforunit(Commons.StrUtil.FormatValue(user["groupid"]));
                Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");
            }
            else
            {
                Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
                DataSet ds = PhoneBook.Group.Department.list(Commons.StrUtil.FormatValue(user["groupid"]), ddlBo.SelectedValue);
                Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");
            }
        }
    }
}